package com

import (
	"time"
	"strconv"
	"github.com/astaxie/beego/orm"
	_ "github.com/lib/pq"
	"beego-restful-server/models"
	"github.com/bugfan/logrus"
)
func NewOrm() orm.Ormer{
	// 解决mysql5.7默认不能groupby
	// o.Raw(`SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'`).Exec()	
	return orm.NewOrm()
}

func InitOrm(user string,pwd string,port string,db string,num string,host string){
	time.Sleep(3e7)
	/*
	* postgres
	*/
	strconv.Atoi(num)
	// orm.RegisterDriver("postgres", orm.DR_Postgres) 
	orm.RegisterDataBase("default", "postgres", "user="+user+" password="+pwd+" dbname="+db+" host="+host+" port="+port+" sslmode=disable")
	models.Syncdb()
	orm.RunSyncdb("default", false, true)
	orm.Debug=true
	orm.DebugLog=orm.NewLog(&LogrusWriter{})

	/*
	* mysql
	*/
	// n,_:=strconv.Atoi(num)
	// log.Println(orm.RegisterDataBase("default", "mysql", user+":"+pwd+"@tcp("+host+":"+port+")/"+db+"?charset=utf8",n))
	// model.Syncdb()
	// orm.RunSyncdb("default", false, true)
	// orm.Debug=false
	// orm.DebugLog=orm.NewLog(&LogrusWriter{})

	logrus.Info("数据库初始化完成! 您使用的是：",user)
}
